package com.lune.job.controller;


import com.lune.job.pojo.Menu;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IMenuService;
import com.lune.job.service.IUserDataService;
import com.lune.job.service.IUserService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserDataService userDataService;

    @ApiOperation(value = "添加菜单")
    @PostMapping("/")
    public RespBean addMenu(@RequestBody Menu menu) {
        if (menu==null){
            return RespBean.error("请输入菜单数据");
        }
        boolean flag = menuService.save(menu);
        if (flag) {
            return RespBean.success("添加菜单成功");
        } else {
            return RespBean.error("添加菜单失败");
        }
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids) {
        if (menuService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("删除菜单成功");
        } else {
            return RespBean.error("删除菜单失败");
        }
    }


    @ApiOperation(value = "修改菜单")
    @PutMapping("/")
    public RespBean update(@RequestBody Menu menu) {
        if (menuService.updateById(menu)) {
            return RespBean.success("修改菜单成功");
        } else {
            return RespBean.error("修改菜单失败");
        }
    }

    @ApiOperation(value = "获取所有菜单")
    @GetMapping("/")
    public List<Menu> getAllMenus() {
        return menuService.list();
    }

    @ApiOperation(value = "根据id获取菜单")
    @GetMapping("/detail")
    public List<Menu> getMenusById( Integer id) {
        return menuService.getMenusById(id);
    }

    @ApiOperation(value = "根据当前登录用户查询菜单")
    @GetMapping("/queryByUser")
    public RespBean query() {
        Menu menu = new Menu();
        //获取当前登录用户 UserData type
        UserData userData = userDataService.getUserData();
        menu.setType(userData.getType());//设置菜单的类型
        List<Menu> menus = menuService.queryByMenu(menu);
        return RespBean.success("根据当前登录用户查询菜单成功", menus);
    }
}
